<?php

namespace app\admin\controller\print_md;
use app\data\model\DataExpress;
use app\data\model\DataUnilateralism;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

class Unilateralism extends Controller
{
    public function index(){
        DataUnilateralism::mQuery()->where(['del'=>0])->layTable(function () {
            $this->title = '快递面单管理';
        }, function (QueryHelper $query) {

        });
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        if(empty($data) === FALSE){
            foreach($data as &$v){
                if($v['is_child'] == 1){
                    //child_name
                    $v['child_name'] = '子单';
                }else{
                    $v['child_name'] = '母单';
                }
                $express = DataExpress::getDetail('name',['id'=>$v['express']]);
                $v['express_name'] = '无信息';
                if(empty($express) === FALSE){
                    $v['express_name'] = $express['name'];
                }
            }
        }

    }
    public function add(){
        if($this->request->isPost()){
            $post = $this->request->post();
            $this->_vali(['title.require' => '参数不能为空！']);
            $this->_vali(['tempId.require' => '参数不能为空！']);
            $this->_vali(['size.require' => '参数不能为空！']);
            $this->_vali(['express.require' => '参数不能为空！']);
            $this->_vali(['partnerId.require' => '参数不能为空！']);
            $this->_vali(['partnerKey.require' => '参数不能为空！']);
            $this->_vali(['payType.require' => '参数不能为空！']);
            DataUnilateralism::create(array(
                'title'          => $post['title'],
                'is_child'        => $post['is_child']??0,
                'tempId'   => $post['tempId'],
                'size'       => $post['size'],
                'express'   => $post['express'],
                'type'   => $post['type'],
                'num'   => $post['num']??1,
                'partnerId'   => $post['partnerId'],
                'partnerKey'   => $post['partnerKey'],
                'partnerSecret'   => $post['partnerSecret'],
                'partnerName'   => $post['partnerName'],
                'net'   => $post['net'],
                'code'   => $post['code'],
                'payType'   => $post['payType'],
            ));
            $this->success('添加面单成功');
        }
        $this->express = DataExpress::getLists('id,name',['del'=>0]);
        $this->fetch();
    }
    public function edit(){
        if($this->request->isPost()){
            $post = $this->request->post();
            $this->_vali(['title.require' => '参数不能为空！']);
            $this->_vali(['tempId.require' => '参数不能为空！']);
            $this->_vali(['size.require' => '参数不能为空！']);
            $this->_vali(['express.require' => '参数不能为空！']);
            $this->_vali(['partnerId.require' => '参数不能为空！']);
            $this->_vali(['partnerKey.require' => '参数不能为空！']);
            $this->_vali(['payType.require' => '参数不能为空！']);
            $id = $this->request->post('id');
            DataUnilateralism::update(array(
                 'title'          => $post['title'],
                'is_child'        => $post['is_child']??0,
                'tempId'   => $post['tempId'],
                'size'       => $post['size'],
                'express'   => $post['express'],
                'type'   => $post['type'],
                'num'   => $post['num']??1,
                'partnerId'   => $post['partnerId'],
                'partnerKey'   => $post['partnerKey'],
                'partnerSecret'   => $post['partnerSecret'],
                'partnerName'   => $post['partnerName'],
                'net'   => $post['net'],
                'code'   => $post['code'],
                'payType'   => $post['payType'],
            ),['id'=>$id]);
            $this->success('编辑面单成功');
        }
        $id = $this->request->get('id');
        $info = DataUnilateralism::getDetail('*',['id'=>$id]);
        $this->express = DataExpress::getLists('id,name',['del'=>0]);
        $this->info = $info;
        $this->id = $id;
        $this->fetch();
    }
    public function del(){
        $id = $this->request->get('id');
        DataUnilateralism::update(array(
                'del'          => 1
            ),['id'=>$id]);
            $this->success('删除面单成功');
    }
}
